$(document).ready(function(){
	findAllRole();
    //黎肇明
    $("#dialog").dialog("destroy");
    $("#dialog-form").dialog({
        autoOpen: false,
        width: 460,
        modal: true,
        resizable: false,
        close: function(){
            $("#user-form input").removeClass('invalid');
            $("#user-form label").remove('.invalid');
            //getPaList();
            //return false;
            $("dialog").dialog("close");
        }
    });
    $('#saveRoleBtn').click(function(){
        $('#dialog-form').dialog('open');
    });
});

function findAllRole(){
    $.ajax({
        type: "post",
        url: "admin/Role_findAllRole.action",
        success: showData
    });
}

function showData(data){
    $("#listRole").empty().hide();
    $("#noneRole").hide();
    $("#loadingRole").hide();
    $.each(data.roles, function(i, n){
        fillDataToHtml(n.RId, n.RName);
    });
    $("#listRole").show();
	$('.editDIV').click(function(){
        setCookie(RId, $(this).next().text());
        setCookie(RName, $(this).next().next().text());
        window.location.href = "role_pmsn_tree.jsp";
    });
    $('.delDIV').click(function(){
		//alert("dfgdfg");	
		//alert($(this).next().text());
        delRoleById($(this).next().text());
	
    });
    
}

function delRoleById(myself){	
	var timestamp = Date.parse(new Date());
	$.get("admin/Role_delRoleById.action?ts="+timestamp+"&RId="+myself,null,delRoleByIdRst);
}

function delRoleByIdRst(data){
	if(data.ajaxRst==SUCCESS){
		findAllRole();
		disRst("删除角色成功");    	
	}else{
		disRst("删除角色失败");
	}
}

function fillDataToHtml(rid, rName){
/*
    var strHtml = "<tr>";
    strHtml += "<td onclick='window.location.href=\"admin/role_getRoleById.action?role.RId=" + rid + "\"'><div><input name='read' type='radio' /></div></td>";
    strHtml += "<td ><div>" + rName + "</div></td>";
    strHtml += "<td ><div class='editDIV'>编辑</div></td>";
    strHtml += "<td ><div class='delDIV'>删除</div></td>";
    strHtml += "</tr>";
    $("#listRole").append(strHtml);
*/
	var strHtml = "<tr>";
    strHtml += "<td><div><input name='read' type='radio' /></div></td>";
    strHtml += "<td ><div>" + rName + "</div></td>";
    strHtml += "<td ><div class='editDIV'>编辑</div><span style='display:none'>";
    strHtml += rid;
    strHtml += "</span><span style='display:none'>"
    strHtml += rName;
    strHtml += "</span></td>";
    strHtml += "<td ><div class='delDIV'>删除</div><span style='display:none'>";
    strHtml += rid;
    strHtml += "</span><span style='display:none'>"
    strHtml += rName;
    strHtml += "</span</td>";
    strHtml += "</tr>";
    $("#listRole").append(strHtml);
}

function closeDlg(data){
    $("#dialog-form").dialog("close");
	if(data.ajaxRst=="success"){		
		disRst("添加角色成功"); 
		findAllRole();
	}else{
		disRst("添加失败成功"); 
	}
	   
}

function disRst(ctn){
	$('#rstSP').show("slow");
	$('#rstSP').text(ctn);
	setTimeout(hiedRst,3000);
}
function hiedRst(){
	$('#rstSP').hide("slow");
}
